<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: 'user-proxy'
});

import { useCrud, useTable, useUpsert, useSearch } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { useI18n } from 'vue-i18n';

const { service } = useCool();
const { t } = useI18n();

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t('昵称'),
			prop: 'nickName',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12
		},
		{
			label: t('手机号'),
			prop: 'phone',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		// {
		// 	label: t('角色'),
		// 	prop: 'role',
		// 	component: { name: 'el-input', props: { clearable: true } },
		// 	span: 12,
		// 	required: true
		// },
		// {
		// 	label: t('代理级别'),
		// 	prop: 'agentLevel',
		// 	component: { name: 'el-input', props: { clearable: true } },
		// 	span: 12
		// },
		{
			label: t('分润比例'),
			prop: 'profitRatio',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		}
	],
	async onSubmit(data, { done, close, next }) {
		next({
			...data,
			role: 1,
			agentLevel: 1
		});

		// done 关闭加载状态
		// close 关闭弹窗
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: 'selection' },
		{ label: t('昵称'), prop: 'nickName', minWidth: 120 },
		{ label: t('手机号'), prop: 'phone', minWidth: 120 },
		{ label: t('代理级别'), prop: 'agentLevel', minWidth: 120 },
		{ label: t('分润比例（%）'), prop: 'profitRatio', minWidth: 150 },
		{
			label: t('创建时间'),
			prop: 'createTime',
			minWidth: 170,
			sortable: 'desc',
			component: { name: 'cl-date-text' }
		},
		{ type: 'op', buttons: ['edit', 'delete'] }
	]
});

// cl-search
const Search = useSearch({
	items: [
		{
			label: ' ',
			prop: 'keyWord',
			component: {
				name: 'el-input',
				props: {
					clearable: true,
					placeholder: t('请输入手机号')
				}
			}
		},
		{
			label: ' ',
			prop: 'agentLevel',
			component: {
				name: 'el-input',
				props: {
					clearable: true,
					placeholder: t('请输入代理级别')
				}
			}
		}
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.user.info,
		async onRefresh(params, { next, done, render }) {
			// 1 默认调用
			const { list } = await next({
				...params,
				role: 1
			});

			// 渲染数据
			render(list);
		}
	},
	app => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
